// 独立再封装个fetch请求
export const chatVoice = async (audio, personaId, sessionId, language, voice) => {
  const formData = new FormData();
  
  formData.append('audio', audio);
  formData.append('personaId', personaId);
  if (sessionId) {
    formData.append('sessionId', sessionId);
  }
  if (language) {
    formData.append('language', language);
  }
  if (voice) {
    formData.append('voice', voice);
  }

  try {
    const response = await fetch('/api/voice/chat', {
      method: 'POST',
      body: formData,
      headers: {
        'Accept': 'audio/wav',
      },
    });

    if (!response.ok) {
      const error = new Error(`请求失败，HTTP状态码: ${response.status}`);
      error.response = response;
      throw error;
    }

    const audioBlob = await response.blob();
    return {
      data: audioBlob,
      headers: {
        'x-session-id': response.headers.get('x-session-id'),
      }
    };

  } catch (error) {
    console.error('fetch 调用失败:', error);
    throw error;
  }
};